package com.example;

import java.util.Timer;
import java.util.TimerTask;

public class B2 {

    private enum State {
        IDLE, READING_INPUT, VERIFYING, LOCKED, FUNCTION_SELECT
    }

    private State currentState;
    private StringBuilder inputPassword;
    private final String correctPassword = "1234"; // 设定的正确密码
    private int failedAttempts;
    private static final int MAX_ATTEMPTS = 3;
    private static final int LOCK_DURATION = 120000; // 锁定时间 120 秒

    public B2() {
        this.currentState = State.IDLE;
        this.inputPassword = new StringBuilder();
        this.failedAttempts = 0;
    }

    // 处理用户输入的字符
    public void enterCharacter(char c) {
        if (currentState == State.IDLE || currentState == State.READING_INPUT) {
            currentState = State.READING_INPUT;
            inputPassword.append(c);

            // 检查密码长度
            if (inputPassword.length() >= 4) {
                verifyPassword();
            }
        }
    }

    // 验证输入密码
    private void verifyPassword() {
        currentState = State.VERIFYING;

        if (inputPassword.toString().equals(correctPassword)) {
            // 密码正确
            currentState = State.FUNCTION_SELECT;
            System.out.println("密码正确，进入功能选择模式。");
            reset();
        } else {
            // 密码错误
            failedAttempts++;
            if (failedAttempts >= MAX_ATTEMPTS) {
                lockControlPanel();
            } else {
                System.out.println("密码错误，请重新输入。");
                resetForRetry();
            }
        }
    }

    // 锁定控制面板
    private void lockControlPanel() {
        currentState = State.LOCKED;
        System.out.println("因连续三次密码错误，控制面板已锁定。");

        // 启动定时器在 120 秒后解锁
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                unlockControlPanel();
            }
        }, LOCK_DURATION);
    }

    // 解锁控制面板
    private void unlockControlPanel() {
        System.out.println("控制面板已解锁，您可以重新尝试输入密码。");
        failedAttempts = 0;
        currentState = State.IDLE;
    }

    // 重置密码输入
    private void reset() {
        inputPassword.setLength(0);
        failedAttempts = 0;
    }

    // 重置但保留失败计数
    private void resetForRetry() {
        inputPassword.setLength(0);
        currentState = State.READING_INPUT;
    }

    // 退出功能选择状态，返回空闲状态
    public void exitFunctionSelect() {
        if (currentState == State.FUNCTION_SELECT) {
            currentState = State.IDLE;
            System.out.println("退出功能选择模式，返回空闲状态。");
        }
    }

    public State getCurrentState() {
        return currentState;
    }

    public static void main(String[] args) {
        B2 controlPanel = new B2();

        // 模拟用户输入过程
        System.out.println("模拟输入正确密码：");
        controlPanel.enterCharacter('1');
        controlPanel.enterCharacter('2');
        controlPanel.enterCharacter('3');
        controlPanel.enterCharacter('4'); // 假设输入正确的密码

        // 模拟功能选择退出
        controlPanel.exitFunctionSelect();

        // 错误尝试锁定示例
        System.out.println("\n模拟密码错误，触发锁定：");
        controlPanel.enterCharacter('0');
        controlPanel.enterCharacter('0');
        controlPanel.enterCharacter('0');
        controlPanel.enterCharacter('0'); // 错误输入四次

        // 错误输入后再次尝试
        System.out.println("\n再次输入错误密码：");
        controlPanel.enterCharacter('0');
        controlPanel.enterCharacter('0');
        controlPanel.enterCharacter('0');
        controlPanel.enterCharacter('0');

        // 错误输入超限后继续模拟
        System.out.println("\n模拟多次错误后锁定控制面板：");
        controlPanel.enterCharacter('0');
        controlPanel.enterCharacter('0');
        controlPanel.enterCharacter('0');
        controlPanel.enterCharacter('0');
    }
}
